package com.asialjim.wechat.official.message.handler.event;

import com.asialjim.wechat.apiservice.constant.WeChatConstants;
import com.asialjim.wechat.message.WeChatMessage;
import com.asialjim.wechat.message.handler.DefaultWeChatMessageHandler;
import com.asialjim.wechat.message.output.WeChatOutputMessage;
import com.asialjim.wechat.msgcrypt.WxBizMsgCryptService;
import com.asialjim.wechat.apiservice.WeChatUserService;
import com.asialjim.wechat.official.message.WeChatOfficialMessageType;
import com.asialjim.wechat.official.message.event.WeChatSubscribeEvent;
import com.asialjim.wechat.official.message.output.WeChatOutputTextMessage;
import com.asialjim.wechat.pojo.user.WeChatUser;
import lombok.extern.slf4j.Slf4j;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: SubscribeEvent ]</i></h2><strong>
 * <p> subscribe event handler
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/16   &nbsp;&nbsp; JDK 8
 */
@Slf4j
public class SubscribeEventHandler extends DefaultWeChatMessageHandler{
    private final WeChatUserService weChatUserService;

    public SubscribeEventHandler(WxBizMsgCryptService cryptService,WeChatUserService userService) {
        super(cryptService);
        this.weChatUserService = userService;
    }

    @Override
    protected WeChatOutputMessage output(WeChatMessage wechatMessage){
        WeChatSubscribeEvent subscribeEvent = (WeChatSubscribeEvent) wechatMessage;

        WeChatUser weChatUser = weChatUserService.weChatUserInfo(subscribeEvent.getFromUserName(), subscribeEvent.getToUserName());
        log.info("\r\n\tWE-CHAT USER:[{}] SUBSCRIBE",weChatUser);

        String eventKey = subscribeEvent.getEventKey();
        // TODO handle event key

        WeChatOutputTextMessage outputTextMessage = WeChatOutputTextMessage.builder().Content("欢迎关注微信公众号").build();
        outputTextMessage.setMsgType(WeChatConstants.OutputXmlMsgType.TEXT);
        return outputTextMessage;
    }

    @Override
    public String messageTypeName() {
        return WeChatOfficialMessageType.SUBSCRIBE_EVENT.name();
    }
}
